package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.EnterpriseProjectService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName ProjectController.java
 * @Description TODO
 * @createTime 2020年03月05日 12:38:00
 */

@Controller
@RequestMapping(value = "system/project")
public class ProjectController {

  @Autowired
  private EnterpriseProjectService enterpriseProjectService;


  /**
   * 添加项目
   */
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(@RequestParam(value = "id", required = false) Integer id,
                      HttpServletRequest request,
                      @RequestParam(value = "pid", defaultValue = "", required = false) String pid,
                      @RequestParam(value = "name", defaultValue = "", required = false) String name) throws Exception {
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    if (null != id) {
      EnterpriseProject enterpriseProject = enterpriseProjectService.findById(id);
      enterpriseProject.setEnterprise(sysUser.getEnterprise());
      enterpriseProject.setCreateDate(new Date());
      enterpriseProject.setIsProArea(0);
      enterpriseProject.setProjectName(name);
      if (pid.equals("")) {
        enterpriseProject.setParentId(-1);
      } else {
        Integer parentId = Integer.parseInt(pid);
        enterpriseProject.setParentId(parentId);
      }
      enterpriseProjectService.saveMerge(enterpriseProject);
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EnterpriseProject.class);
      detachedCriteria.add(Restrictions.eq("enterprise", sysUser.getEnterprise()));
      detachedCriteria.add(Restrictions.eq("parentId", -1));
      List<EnterpriseProject> enterpriseProjectList = enterpriseProjectService.findByConditions(detachedCriteria);
      request.getSession().setAttribute("enterprise", enterpriseProjectList);
      return Layui.data(0, "更新成功", 1, Layui.addList(enterpriseProject));
    } else {
      EnterpriseProject enterpriseProject =new EnterpriseProject();
      enterpriseProject.setCreateDate(new Date());
      enterpriseProject.setEnterprise(sysUser.getEnterprise());
      enterpriseProject.setIsProArea(0);
      enterpriseProject.setProjectName(name);
      if (pid.equals("")) {
        enterpriseProject.setParentId(-1);
      } else {
        Integer parentId = Integer.parseInt(pid);
        enterpriseProject.setParentId(parentId);
      }
      enterpriseProjectService.saveOrUpdate(enterpriseProject);
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EnterpriseProject.class);
      detachedCriteria.add(Restrictions.eq("enterprise", sysUser.getEnterprise()));
      detachedCriteria.add(Restrictions.eq("parentId", -1));
      List<EnterpriseProject> enterpriseProjectList = enterpriseProjectService.findByConditions(detachedCriteria);
      request.getSession().setAttribute("enterprise", enterpriseProjectList);
      return Layui.data(0, "更新成功", 1, Layui.addList(enterpriseProject));
    }


  }


  @ResponseBody
  @RequestMapping(value = "findAll")
  public Layui findAll() {
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
    criteria.add(Restrictions.eq("enterprise", sysUser.getEnterprise()));
    List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
    return Layui.data(0, "查询成功", enterpriseProjectService.getTotalCount(), list);

  }


  /**
   * 添加子节点
   */
  @ResponseBody
  @RequestMapping(value = "addOne")
  public Layui addOne(@RequestParam(value = "pid", required = false) Integer pid,
                      @RequestParam(value = "id", required = false) Integer id,
                      HttpServletRequest request,
                      @RequestParam(value = "name", required = false) String name) {

    EnterpriseProject classify;
    if (null != id) {
      classify = enterpriseProjectService.findById(id);
    } else {
      classify = new EnterpriseProject();
      SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
      classify.setParentId(pid);
      classify.setEnterprise(sysUser.getEnterprise());
    }
    classify.setProjectName(name);
    classify.setIsProArea(1);
    enterpriseProjectService.saveMerge(classify);
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EnterpriseProject.class);
    detachedCriteria.add(Restrictions.eq("enterprise", sysUser.getEnterprise()));
    detachedCriteria.add(Restrictions.eq("parentId", -1));
    List<EnterpriseProject> enterpriseProjectList = enterpriseProjectService.findByConditions(detachedCriteria);
    request.getSession().setAttribute("enterprise", enterpriseProjectList);
    return Layui.data(0, "更新区域成功", 1, Layui.addList(classify));
  }


  /**
   * 选择切换项目
   */
  @ResponseBody
  @RequestMapping(value = "switchproject")
  public Layui switchproject(@RequestParam(value = "id", required = false) Integer id, HttpServletRequest req) {
    EnterpriseProject id1 = enterpriseProjectService.findById(id);
    req.getSession().setAttribute("project", id1);
    return Layui.data(0, "切换项目成功", 1, null);
  }

  /**
   * 选择切换项目
   */
  @ResponseBody
  @RequestMapping(value = "switcharea")
  public Layui switcharea(@RequestParam(value = "id", required = false) Integer id, HttpServletRequest req) {
    EnterpriseProject id1 = enterpriseProjectService.findById(id);
    req.getSession().setAttribute("area", id1);
    return Layui.data(0, "切换区域成功", 1, null);
  }
}
